/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Classifier;

import DataBase.WekaDbReader;
import Training.InstancesFilter;
import java.util.ArrayList;
import weka.classifiers.meta.Vote;
import weka.classifiers.trees.J48;

import weka.core.Instances;

/**
 *
 * @author fabio
 */
public class Classifier {

    private String filePath;
    private String filePathModels;
    //private Vote classifier;
    private J48 classifier;
    private ArrayList<Integer> idList = new ArrayList<Integer>();

    //cenas weka para tratar mail individual e o classificar
    //carregar mail individual
    public Classifier(String filePath) throws Exception {
        this.filePath = filePath;
        this.filePathModels = filePath + "Modelos/";
        classifier = loadlassModel("VoteClassifier.Model");
    }

    private J48 loadlassModel(String filename) throws Exception {
        J48 c = (J48) weka.core.SerializationHelper.read(filePath + filename);
        c.toString();
        return c;
    }

    public ArrayList<Integer> emailClassifier(int numMails) throws Exception {
        ArrayList<Integer> classifi = new ArrayList<Integer>();
        double clsLabel;
        //Vote voteClassifier = classifier;
        J48 voteClassifier = classifier;
        WekaDbReader dbReader = new WekaDbReader();
        String sqlQuery = "select * from viewmails where spam = -1 order by id desc limit 0, " + numMails;
        Instances instances = dbReader.loadData(sqlQuery);
        for (int i = 0; i < instances.numInstances(); i++) {
            // System.out.println(instances.instance(i).value(0));
            idList.add((int) instances.instance(i).value(0));
        }
        InstancesFilter filter = new InstancesFilter();
        instances = filter.filterInstances(instances);
        System.out.println(classifier.toString());
        //System.out.println(instances.toString());
        instances.setClassIndex(instances.numAttributes() - 1);
        for (int i = 0; i < instances.numInstances(); i++) {
            System.out.println(instances.instance(i).toString());
            clsLabel = voteClassifier.classifyInstance(instances.instance(i));
            instances.instance(i).setClassValue(clsLabel); 
            System.out.println(instances.instance(i).toString());
            //clsLabel
            System.out.println(clsLabel);
            classifi.add((int) clsLabel);
        }
        return classifi;
    }

    public ArrayList<Integer> getIdList() {
        return idList;
    }
}
